import "../../lib/ramda.js";

const STEP_MS_MIN = 500
const STEP_MS_MAX = 10000

const State = {
    engine_usable: false,
    step_ms_max: STEP_MS_MIN,
}

const Out = {
    reply(...args){
        self.postMessage(args.join(' '))
    },
    reply_ok(...args){
        Out.reply("ok", ...args)
    },
    report_error(msg){
        self.postMessage(`[ERROR] ${msg}`)
    },
}

const Take = {}

Take["step"] = (pcur_str, state_str)=>{
    Out.reply("step pass")
}

Take["valuation"] = (state_str)=>{
    Out.reply_ok("valuation", "0")
}

Take["set-step-ms-max"] = (a)=>{
    const n = Number(a)
    if (n >= STEP_MS_MIN && n <= STEP_MS_MAX) {
        State.step_ms_max = a
        if (State.engine_usable) {
        }
        Out.reply_ok("set-step-ms-max", State.step_ms_max)
    }
}

Take["reset"] = ()=>{
    console.log("(reset)")
}

Take["raii"] = ()=>{
    console.log("(raii)")
}

;(()=>{
    self.onmessage = function(e){
        const tokens = e.data.split(' ')
        if (tokens.length > 0){
            const head = tokens[0]
            if (Take.hasOwnProperty(head)) {
                Take[head](...(tokens.slice(1)))
            } else {
                Out.report_error(e.data)
            }
        }
    }
    Out.reply("ready")
})()
